from django.contrib.auth.backends import ModelBackend
from django.db.models import Q

from six_it.models import User


class WHO(ModelBackend):
    def authenticate(self, request, username=None, password=None, **kwargs):
        try:
            user = User.objects.get(Q(username=username) | Q(mobile=username))
        except:
            user = None

        if user and user.check_password(password):
            return user
        raise ValueError('用户不存在')
